package cn.suxiaoxiang.suwebverify.config;

import cn.suxiaoxiang.suwebverify.SuWebVerify;
import cn.suxiaoxiang.suwebverify.api.EncryptionKey;
import cn.suxiaoxiang.suwebverify.api.Login;
import cn.suxiaoxiang.suwebverify.api.register;
import cn.suxiaoxiang.suwebverify.api.BackgroundImages;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.server.handler.ContextHandler;
import java.io.File;

/**
 * WebServer - 网页服务器启动类
 *
 * @author 程序员-Su_xiaoxiang
 * @date 2025/4/7 11:15
 */
public class WebServer {
    // 注入server
    private Server server;
    // 注入插件类
    private final SuWebVerify thisplugin;

    /**
     * 网页服务器
     *
     * @param plugin 插件
     */
    public WebServer(SuWebVerify plugin) {
        this.thisplugin = plugin;
    }

    public void startServer() {
        int port = thisplugin.getConfig().getInt("port");
        server = new Server(port);

        // 处理静态资源 (前端页面)
        ResourceHandler resourceHandler = new ResourceHandler();
        resourceHandler.setDirectoriesListed(false);
        resourceHandler.setResourceBase(getClass().getClassLoader().getResource("web").toExternalForm());
        // 设置默认首页文件为 Login.html
        resourceHandler.setWelcomeFiles(new String[]{"html/login.html"});

        // 处理插件目录下的图片文件
        ResourceHandler imagesHandler = new ResourceHandler();
        imagesHandler.setDirectoriesListed(false);
        imagesHandler.setResourceBase(new File(thisplugin.getDataFolder(), "web/images").getAbsolutePath());
        imagesHandler.setPathInfoOnly(true);
        ContextHandler imagesContext = new ContextHandler("/images");
        imagesContext.setHandler(imagesHandler);

        // 处理 API 和 WebSocket
        ServletContextHandler servletContext = new ServletContextHandler(ServletContextHandler.SESSIONS);
        servletContext.addServlet(new ServletHolder(new Login(thisplugin)), "/api/login");
        servletContext.addServlet(new ServletHolder(new register(thisplugin)), "/api/register");
        servletContext.addServlet(new ServletHolder(new EncryptionKey(thisplugin)), "/api/encryption-key");
        servletContext.addServlet(new ServletHolder(new BackgroundImages(thisplugin)), "/api/backgrounds");

        // 组合所有处理器
        HandlerList handlers = new HandlerList();
        handlers.addHandler(resourceHandler);
        handlers.addHandler(imagesContext);
        handlers.addHandler(servletContext);

        server.setHandler(handlers);

        try {
            server.start();
            thisplugin.getLogger().info("Web 服务器已启动，访问地址：http://您的服务器IP:" + port);
            // 创建图片目录
            File imagesDir = new File(thisplugin.getDataFolder(), "web/images");
            if (!imagesDir.exists()) {
                imagesDir.mkdirs();
                thisplugin.getLogger().info("创建背景图片目录: " + imagesDir.getAbsolutePath());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void stopServer() {
        try {
            if (server != null) {
                server.stop();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
